
#pragma once

#include <d3d11.h>
#include <dxgi1_2.h>
#include <DirectXMath.h>
#include <thread>
#include <atomic>
#include <cstdint>

#include "Poco/Timer.h"


class VideoCaptureObserver
{
public:
	virtual void onVideoCaptureData(void* data, int len) = 0;
};

class DesktopCapture
{
public:
	DesktopCapture(const int screenId=0, VideoCaptureObserver* p=nullptr);
	~DesktopCapture();
	bool Init();
	void GetVideoInfo(int& w, int& h, int& fps);
	void Start();
	void Stop();
private:
	void onGetFrame(Poco::Timer& timer);
	bool SaveFrameAndCallback(ID3D11Texture2D* texture);
	void onFrame();
private:
	UINT						m_DesktopID;
	Poco::Timer					m_timer;
	int							m_width;
	int							m_heigh;
	int							m_fps;
	VideoCaptureObserver*		m_ob = nullptr;
	bool						m_hasFrame = false;
	ID3D11Device*				m_Device;
	ID3D11DeviceContext* m_DeviceContext;
	DXGI_OUTPUT_DESC			m_OutputDesc;
	IDXGIOutputDuplication* m_DeskDupl;
	ID3D11Texture2D* m_AcquiredDesktopImage;
	std::unique_ptr<uint8_t[]>	m_pI420Buffer;
	int							m_nI420Len;
};